﻿using Common.Enums;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Extension
{
    /// <summary>
    /// decimal数据类型扩展
    /// </summary>
    /// <remarks>
    /// <![CDATA[
    /// 扩展一些常用的判断、数据转换等
    /// ]]>
    /// </remarks>
    public static class DecimalExtension
    {
        /// <summary>
        /// 将decimal转换成货币显示格式
        /// 10.123 转换为 ￥10.12
        /// </summary>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static string ToRMBFormat(this decimal amount)
        {
            // return dec.ToString("C2", System.Globalization.CultureInfo.CreateSpecificCulture("en-us"));//英文
            // return dec.ToString("C2", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));//zh-CN、zh-HK、zh-TW、zh-MO 、 zh-SG
            //return string.Concat("RMB ", dec.ToString("N2"));
            return string.Concat(amount.ToString("N2"));
        }
        /// <summary>
        /// 将decimal转换成货币显示格式
        /// 10.123 转换为 ￥10.12
        /// </summary>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static string ToCurrencyFormat(this decimal amount)
        {
            return string.Concat("￥", amount.ToString("N2"));
        }

        /// <summary>
        /// 将decimal转换为N元格式显示
        /// 10.12 转换为 10.12元
        /// </summary>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static string ToYuanFormat(this decimal amount)
        {
            return string.Concat(amount.ToString("N2"), "元");
        }

        /// <summary>
        /// 转换为万元为单位
        /// </summary>
        /// <param name="amount">金额</param>
        /// <param name="baseAmount">基数，默认为10000</param>
        /// <param name="decimals">保留小数点位数，默认为2位</param>
        /// <returns></returns>
        public static string ToWanYuanFormat(this decimal amount, decimal baseAmount = 10000, int decimals = 2)
        {
            //大于1万
            if (amount > baseAmount)
            {
                decimal temp = amount / baseAmount;
                return $"{Math.Round(temp, decimals)}万";
            }
            return amount.ToString("N" + decimals);
        }
        /// <summary>
        /// 四舍五入保留两位小数
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static decimal Format(this decimal d)
        {
            return Format(d, 2, DecimalFormat.AwayFromZero);
        }
        /// <summary>
        /// 格式化decimal
        /// </summary>
        /// <param name="d"></param>
        /// <param name="scale"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static decimal Format(this decimal d, int scale, DecimalFormat format)
        {
            if (scale < 0)
            {
                throw new ArgumentException("小数位精度不允许小于零");
            }
            if (scale > 10)
            {
                throw new ArgumentException("小数位数超过精度允许范围");
            }
            switch (format)
            {
                case DecimalFormat.AwayFromZero:
                    return decimal.Round(d, scale, MidpointRounding.AwayFromZero);
                case DecimalFormat.ToEven:
                    return decimal.Round(d, scale, MidpointRounding.ToEven);
                case DecimalFormat.Carry:
                    var zoom = (int)Math.Pow(10, scale);
                    var temp = d * zoom;
                    temp = decimal.Ceiling(temp);
                    return temp / zoom;
                case DecimalFormat.Trucation:
                    return d.TruncateValue(scale);
                default:
                    return d;
            }
        }
    }
}
